package com.heima.controller;

import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.heima.model.article.pojos.Article;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.service.ArticleService;
import freemarker.template.TemplateException;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @author chenshan
 */
@RestController
@RequestMapping("api/v1/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /**
     * APP端文章首页数据请求
     *
     * @param homeDto homeDto
     * @return ResponseResult
     */
    @PostMapping("load")
    public ResponseResult load(@RequestBody ArticleHomeDto homeDto) {
        return articleService.load(homeDto);
    }

    /**
     * 下拉加载最新数据
     *
     * @param homeDto homeDto
     * @return ResponseResult
     */
    @PostMapping("loadnew")
    public ResponseResult loadNew(@RequestBody ArticleHomeDto homeDto) {
        return articleService.loadNew(homeDto);
    }

    /**
     * 上拉加载更多数据
     *
     * @param homeDto homeDto
     * @return ResponseResult
     */
    @PostMapping("loadmore")
    public ResponseResult loadMore(@RequestBody ArticleHomeDto homeDto) {
        return articleService.loadMore(homeDto);
    }

    /**
     * 发布文章
     *
     * @param articleDto 文章内容
     * @return ResponseResult
     */
    @PostMapping("save")
    public ResponseResult saveArticle(@RequestBody ArticleDto articleDto) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, InternalException, TemplateException, XmlParserException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException {
        return articleService.saveArticle(articleDto);
    }

    /**
     * 根据文章id查询文章内容（es测试）
     *
     * @param articleId 文章id
     * @return ResponseResult
     */
    @GetMapping("findOneById/{id}")
    public ResponseResult findOneById(@PathVariable("id") Long articleId) {
        return articleService.findOneById(articleId);
    }

    /**
     * 查询所有文章 已经上架且没有被删除（es测试）
     *
     * @return ResponseResult
     */
    @GetMapping("findAll")
    public ResponseResult findAll() {
        return articleService.findAll();
    }

    /**
     * 加载文章行为 - 数据回显
     *
     * @param infoDto 文章信息
     * @return ResponseResult
     */
    @PostMapping("load_article_behavior")
    public ResponseResult loadArticleBehavior(@RequestBody ArticleInfoDto infoDto) {
        return articleService.loadArticleBehavior(infoDto);
    }

    /**
     * 根据频道id查询文章（查询此频道是否有文章）
     *
     * @param id 频道id
     * @return ResponseResult
     */
    @PostMapping("findArticleByChannelId")
    public ResponseResult findArticleByChannelId(@RequestParam Integer id) {
        return articleService.findArticleByChannelId(id);
    }

    /**
     * 根据id修改文章
     *
     * @return boolean
     */
    @PostMapping("updateArticle")
    public boolean updateArticle(@RequestBody Article article) {
        return articleService.updateById(article);
    }

    /**
     * 根据id查找文章
     *
     * @return boolean
     */
    @PostMapping("findArticleById")
    public Article findArticleById(@RequestParam Long id) {
        return articleService.getById(id);
    }

    /**
     * 查询最近5天的文章数据（未下架 未删除）
     *
     * @return boolean
     */
    @PostMapping("selectFiveDaysArticle")
    public ResponseResult selectFiveDaysArticle() {
        return articleService.selectFiveDaysArticle();
    }
}
